<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri ="http://java.sun.com/jsp/jstl/functions"%>
<div class="page-header">
	<h3>添加部门</h3>
</div>
<div class="input-group">
	<input name="addName" type="text" class="form-control"
		placeholder="添加部门"> <span class="input-group-btn">
		<button id="addDepartment" class="btn btn-default" type="button">确认</button>
	</span>
</div>
<div class="page-header">
	<h3>部门列表</h3>
</div>
<table class="table table-striped table-hover .table-condensed">
	<thead>
		<tr>
			<th>名称</th>
			<th>操作</th>
		</tr>
	</thead>
	<tbody>
		<c:forEach var="department" items="${result.object}">
			<tr>
				<td>${fn:escapeXml(department.name)}</td>
				<td><a class="editDepartment btn btn-default"
					dname="${fn:escapeXml(department.name)}" did="${department.id}">修改</a> <a
					class="delDepartment btn btn-default" did="${department.id}">删除</a></td>
			</tr>
		</c:forEach>
	</tbody>
</table>
<div id="editPanel" class="panel panel-default"
	style="display: none; margin-bottom: 0;">
	<div class="panel-heading">
		<h3 class="panel-title">修改名称</h3>
	</div>
	<div class="panel-body">
		<input name="editId" type="hidden" placeholder="部门名称" /> <input
			name="editName" type="text" /> <br /> <br /> <a
			id="editDepartment" class="btn btn-default pull-right">确认</a>
	</div>
</div>
<script>
	$(function() {
		$("#addDepartment").click(function() {
			var addName = $("input[name=addName]").val();
			if (addName == null || addName == "") {
				layer.tips('名称不能为空', 'input[name=addName]', {
					tips : 3
				});
				return false;
			}
			$.post("${basePath}/product/department/add", {
				name : addName
			}, function(data) {
				if (data.result.success) {
					layer.msg(data.result.message, {
						icon : 1,
						time : 1000
					}, function() {
						$("#rightDiv").load("${basePath}/product/department");
					});
				} else if (data.result.message == "部门名已存在") {
					layer.tips(data.result.message, 'input[name=addName]', {
						tips : 3
					});
				} else {
					layer.msg(data.result.message, {
						icon : 2
					});
				}
			});
		});
		$(".editDepartment").click(function() {
			$("input[name=editName]").val($(this).attr("dname"));
			$("input[name=editId]").val($(this).attr("did"));
			window.editLayer = layer.open({
				type : 1,
				title : false,
				closeBtn : 1,
				shadeClose : true,
				content : $('#editPanel')
			});
		});
		$("#editDepartment").click(
				function() {
					var editId = $("input[name=editId]").val();
					var editName = $("input[name=editName]").val();
					if ($("input[name=editName]").val() == null
							|| $("input[name=editName]").val() == "") {
						layer.tips('名称不能为空', 'input[name=editName]', {
							tips : 3
						});
					} else {
						$.post("${basePath}/product/department/update", {
							id : editId,
							name : editName
						}, function(data) {
							if (data.result.success) {
								layer.msg(data.result.message, {
									icon : 1,
									time : 1000
								}, function() {
									layer.close(editLayer);
									$("#rightDiv").load(
											"${basePath}/product/department");
								});
							} else if (data.result.message == "部门名已存在") {
								layer.tips(data.result.message, 'input[name=editName]', {
									tips : 3
								});
							} else {
								layer.msg(data.result.message, {
									icon : 2
								});
							}
						});
					}
				})
		$(".delDepartment").click(
				function() {
					var deleteId = $(this).attr("did");
					layer.confirm('确定要删除', {
						icon : 3,
						title : '提示'
					}, function(index) {
						$.post("${basePath}/product/department/delete", {
							id : deleteId
						}, function(data) {
							if (data.result.success) {
								layer.msg(data.result.message, {
									icon : 1,
									time : 1000
								}, function() {
									$("#rightDiv").load(
											"${basePath}/product/department");
								});
							} else {
								layer.msg(data.result.message, {
									icon : 2
								});
							}
						});
						layer.close(index);
					});
				});
	});
</script>
